import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Main',
    component: Main,
    children: [
      {
        path: '/',
        name: 'home',
        component: () => import('@/views/Home/Home')
      },
      {
        path: '/mall',
        name: 'mall',
        component: () => import('@/views/Mall/Mall')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('@/views/User/User')
      }
    ]
  }, {
    path: '/login',
    component: () => import('@/views/Login/Login')
  }, {
    path: '/register',
    component: () => import('@/views/Login/Register')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const needAuthPaths = ['/', '/home', '/mall', '/user']
router.beforeEach((to, from, next) => {
  // 防止刷新后vuex里丢失token
  // store.commit('getToken')
  // // 防止刷新后vuex里丢失标签列表tagList
  // store.commit('getMenu')
  // let token = store.state.user.token
  // 过滤登录页，因为去登陆页不需要token(防止死循环)
  // if (!token && to.name !== 'login') {
  //   next({ name: 'login' })
  // } else {
  //   next()
  // }
  if (needAuthPaths.some(item => item === to.path)) {
    if (localStorage.token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router



// import Vue from 'vue'
// import Router from 'vue-router'
// const originPush = Router.prototype.push
// Router.prototype.push = function push(location) {
//   return originPush.call(this, location).catch(err => err)
// }
// Vue.use(Router)

// export default new Router({
//   routes: [
//     {
//       path: '/',
//       name: 'login',
//       component: () => import('@/views/Login/Login')
//     }
//   ]
// })